<?php

namespace backend\controllers;

use Yii;
use backend\models\SysItem;
use backend\models\SysItemSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\GrpClass;
use yii\helpers\ArrayHelper;

use yii\web\UploadedFile;
use backend\models\UploadForm;
use PHPThumb\GD;
use backend\models\Image;
use backend\models\Fabric;
use backend\models\Material;
use backend\models\ItemPreview;
use backend\models\ItemPreviewAngle;

use yii\web\ForbiddenHttpException;


use liangzoe\mobilesmser\TextSmser;
use backend\models\PrfType;
use backend\models\GrpColor;
use backend\models\GrpSpace;
use backend\models\GrpStyle;
use backend\models\GrpPrice;
use backend\models\SysItemColor;
use backend\models\SysItemSpace;
use backend\models\SysItemStyle;
use backend\models\SysItemPrices;
use backend\models\SysItemPlace;
use backend\models\SysItemSery;
use backend\models\SysItemPricesSearch;
use backend\models\GrpSery;
use backend\models\SelectImageForm;
use backend\models\ItemPreviewDemo;


/**
 * SysItemController implements the CRUD actions for SysItem model.
 */
class SysItemDemoController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    public function beforeAction($action)
    {
    	//如果未登录，则直接跳转
    	if(Yii::$app->user->isGuest){
    		return $this->redirect(['/site/login']);
    	}
    	 
    	 
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	if(\Yii::$app->user->can('item-manage')){
    		return true;
    	}else{
    		throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
    	}
    }
    /**
     * Lists all SysItem models.
     * @return mixed
     */

    public function actionIndex()
    {
        $searchModel = new SysItemSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single SysItem model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
    	$this->layout = 'main_small';
    	$model = $this->findModel($id);
    	
    	$color = SysItemColor::find()->where(['item_id' =>$id])->all();
    	foreach ($color as $value){
    		$model->color_list .= $value->grpName->name.',';
    	}
    	$style = SysItemStyle::find()->where(['item_id' =>$id])->all();
    	foreach ($style as $value){
    		$model->style_list .= $value->grpName->name.',';
    	}
    	$space = SysItemSpace::find()->where(['item_id' =>$id])->all();
    	foreach ($space as $value){
    		$model->space_list .= $value->grpName->name.',';
    	}
    	$place = SysItemPlace::find()->where(['item_id' =>$id])->all();
    	foreach ($place as $value){
    		$model->place_list .= $value->grpName->name.',';
    	}
    	$sery = SysItemSery::find()->where(['item_id' =>$id])->all();
    	foreach ($sery as $value){
    		$model->sery_list .= $value->grpName->name.',';
    	}
    	
    	$status_array =array('0'=>'编辑','1'=>'上架','2'=>'下架','3'=>'删除');  	
    	$model->app_return = isset($status_array[$model->app_return])?$status_array[$model->app_return]:$model->app_return;
    	
        return $this->render('view', [
            'model' => $model,
        ]);
    }
    /**
     * 从数据库中查出数据并转换为数组
     * @param unknown $item_id
     * @param unknown $model
     * @param unknown $field
     * @return multitype:
     */
    public function get_ext($model,$field)
    {
    	$arr = array();
    	foreach ($model as $value){
    		array_push($arr, $value[$field]);
    	}
    	return $arr;
    }

    public function add_ext($item_id,$color_list,$style_list,$space_list,$place_list,$sery_list)
    {
    	if(!empty($color_list)){
	    	foreach ($color_list as $value)
	    	{
	    		$model_color = new SysItemColor();
	    		$model_color->item_id = $item_id;
	    		$model_color->color_id = $value;
	    		$model_color->save();
	    	}
    	}
    	if(!empty($style_list)){
    		foreach ($style_list as $value)
    		{
    			$model_style = new SysItemStyle();
    			$model_style->item_id = $item_id;
    			$model_style->style_id = $value;
    			$model_style->save();
    		}
    	}
    	if(!empty($space_list)){
	    	foreach ($space_list as $value)
	    	{
	    		$model_space = new SysItemSpace();
	    		$model_space->item_id = $item_id;
	    		$model_space->space_id = $value;
	    		$model_space->save();
	    	}
    	}
    	if(!empty($place_list)){
    		foreach ($place_list as $value)
    		{
    			$model_place = new SysItemPlace();
    			$model_place->item_id = $item_id;
    			$model_place->place_id = $value;
    			$model_place->save();
    		}
    	}
    	if(!empty($sery_list)){
    		foreach ($sery_list as $value)
    		{
    			$model_sery = new SysItemSery();
    			$model_sery->item_id = $item_id;
    			$model_sery->sery_id = $value;
    			$model_sery->save();
    		}
    	}

    }
    /**
     * Creates a new SysItem model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
    	$this->layout = 'main_small';
        $model = new SysItem();
		 
		
		
        if ($model->load(Yii::$app->request->post())) {
        	//echo '<script type="text/javascript">opener.location.reload();</script>';
        	if(empty($model->class_id)){$model->class_id = $model->class2;}
        	$model->save();
        	$this->add_ext($model->id, $model->color_list, $model->style_list, $model->space_list, $model->place_list, $model->sery_list);

            return $this->redirect(['view', 'id' => $model->id]);
           
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    
    /**
     * Updates an existing SysItem model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
    	$this->layout = 'main_small';
        $model = $this->findModel($id);

    	$old_class_id = $model->class_id;
    	$old_brand_id = $model->brand_id;
        //显示已选中的checkbox
        $color = SysItemColor::find()->where(['item_id' =>$id])->asArray()->all();     
        $style = SysItemStyle::find()->where(['item_id' =>$id])->asArray()->all();      
        $space = SysItemSpace::find()->where(['item_id' =>$id])->asArray()->all();
        $place = SysItemPlace::find()->where(['item_id' =>$id])->asArray()->all();
        $sery = SysItemSery::find()->where(['item_id' =>$id])->asArray()->all();
     
        $model->color_list = $this->get_ext($color, 'color_id');
        $model->style_list = $this->get_ext($style, 'style_id');
        $model->space_list = $this->get_ext($space, 'space_id');
        $model->place_list = $this->get_ext($place, 'place_id');
        $model->sery_list = $this->get_ext($sery, 'sery_id');

		
        if ($model->load(Yii::$app->request->post())) {
        	if(empty($model->class_id)){$model->class_id = $model->class2;}
        	$model->save();
        	//echo '<script type="text/javascript">alert("刷新页面");opener.location.reload();</script>';
        	//修改时先删除item_id的数据
        	SysItemColor::deleteAll(['item_id' => $model->id]);
        	SysItemStyle::deleteAll(['item_id' => $model->id]);
        	SysItemSpace::deleteAll(['item_id' => $model->id]);
        	SysItemPlace::deleteAll(['item_id' => $model->id]);
        	SysItemSery::deleteAll(['item_id' => $model->id]);
        	
        	$this->add_ext($model->id, $model->color_list, $model->style_list, $model->space_list, $model->place_list, $model->sery_list);
        	
        	$new_class_id = $model->class_id;
        	$new_brand_id = $model->brand_id;
        	if($old_class_id != $new_brand_id || $old_brand_id != $new_brand_id)
        	{
        		$model_g = $this->findModel($id);
        		$model_g->gjj_code = $this->update_gjj_code($id);
        		$model_g->save();
        	}
        		
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
        	
        	if(!empty($model->class_id)){  //class_id不为空时
        		$class_id = $model->class_id;
        		$pid = GrpClass::findOne(['id'=>$class_id])->father;
        		$ppid = GrpClass::findOne(['id'=>$pid])->father;
        		
        		$model->class1 = $ppid;
        		$model->class2 = $pid;
        		
        	}
        	if(strpos($model->class_id, '000000'))//只有二级
        	{
        		$model->class2 = $model->class_id;
        		$model->class1 = GrpClass::findOne(['id'=>$model->class2])->father;
        		$model->class_id = null;
        	}
        	
            return $this->render('update', [
                'model' => $model,
          		
            ]);
        }
    }

    /**
     * Deletes an existing SysItem model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the SysItem model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SysItem the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SysItem::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /**
     * ajax选择类别
     * @param unknown $id
     */
    public function actionSelectclass1($id)
    {
    	$option_class2 = '';
    	$option_class_id = '';
    	$result = GrpClass::findAll(['father'=>$id]);
    	if ($result) {
    		foreach ($result as $value) {
    			$option_class2 .= '<option value="' . $value['id'] . '">' . $value['name'] . '</option>';
    			if(!empty($value['id']))
    			{
    				$result2 = GrpClass::findAll(['father'=>$value['id']]);
    				if($result2){
    					foreach ($result2 as $value)
    					{
    						$option_class_id .= '<option value="' . $value['id'] . '">' . $value['name'] . '</option>';
    					}
    				}
    				else{
    					$option_class_id .= '<option value="0">暂无</option>';
    				}
    							
    			}
    		}
    	} else {
    		$option_class2 .= '<option value="0">暂无</option>';
    	}
    	$arr_option =  array('class2'=>$option_class2,'class_id'=>$option_class_id);
    	echo  json_encode($arr_option); //以json数据返回
    
    }
    /**
     * ajax选择类别
     * @param unknown $id
     */
    public function actionSelectclass2($id)
    {
    	$option = '';
    	$result = GrpClass::findAll(['father'=>$id]);
        if ($result) {
            foreach ($result as $value) {
                $option .= '<option value="' . $value['id'] . '">' . $value['name'] . '</option>';
            }
        } else {
            $option .= '<option value="0">暂无</option>';
        }

        echo $option;
        
    }
    public function actionSelectsery($brand_id)
    {
    	$checkbox = '';
    	$result = GrpSery::findAll(['brand_id'=>$brand_id]);
    		if($result)
    		{
    			foreach ($result as $key => $value) {
    				$checkbox .= '<label><input type="checkbox" name="SysItem[sery_list][]" value="'.$value['id'].'">'.$value['name']."</label>";
    			}
    		}else{
    			$checkbox .= '暂无';
    		}
    		echo $checkbox;
    
    }

    public function actionImagedefault($item_id,$item_name,$class_name)
    {	
    	$this->layout = 'main_small';
    	$model = new SelectImageForm();
    	$model->scenario = 'no_need_file_name';
    	
    	if ($model->load(Yii::$app->request->post())) {
    		if($model->big_img_id == null && $model->b_t_s_img_id == null )
    		{
    			echo '<span class="btn btn-info">大图或大中小图必须选择一个</span>';
    		}
    		else{

    			$model_item = SysItem::findOne(['id'=>$item_id]);
    			if($model->b_t_s_img_id == null)
    			{
    				if($model->big_img_id != null ) $model_item->img_url_id = $model->big_img_id;    			
	    			if($model->thumb_img_id != null ) $model_item->img_thumb_id = $model->thumb_img_id;    			
	    			if($model->small_img_id != null ) $model_item->img_small_id = $model->small_img_id;
    			}
    			else
    			{
    			
    				$url = Image::findOne($model->b_t_s_img_id)->url;
    				
    				$model_image = new Image();
        			$_arr_img = $model_image->get_img_id($url);
    				  				
    				$model_item->img_url_id = $_arr_img['big_img_id'];
    				$model_item->img_thumb_id = $_arr_img['thumb_img_id'];
    				$model_item->img_small_id = $_arr_img['small_img_id'];   				
    			}
    			
    			if($model_item->save())
    			{
    				//echo $arr['img_show'];
    			}
    			else
    			{
    				//echo '上传失败';
    			}
    		}	
    		
    	}
    	
    	return $this->render('imagedefault', [
    			'model' => $model,
    			]);
    }
    

/**
 * 上传面料
 * @param unknown $id
 * @return Ambigous <string, string>
 */
    
      public function actionImagefabric($item_id,$item_name,$class_name)
    {	
    	$this->layout = 'main_small';
    	$model = new SelectImageForm();
    	$model->scenario = 'need_file_name';
    	
    	$name = '';
    	if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
    		if($model->big_img_id == null && $model->b_t_s_img_id == null )
    		{
    			echo '<span class="btn btn-info">大图或大中小图必须选择一个</span>';
    		}
    		
    		$name = $model->name;
    		$img_url_id = '';
    		$img_thumb_id = '';
    		$img_small_id = '';
    		
    		if ($model->validate() && ($model->big_img_id != null || $model->b_t_s_img_id != null) ) {
    			
    			
    			if($model->b_t_s_img_id == null)
    			{
    				if($model->big_img_id != null ) $img_url_id = $model->big_img_id;
    				if($model->thumb_img_id != null ) $img_thumb_id = $model->thumb_img_id;
    				if($model->small_img_id != null ) $img_small_id = $model->small_img_id;
    			}
    			else
    			{
    				 
    				$url = Image::findOne($model->b_t_s_img_id)->url;    			
    				$model_image = new Image();
        			$_arr_img = $model_image->get_img_id($url); 			
    				$img_url_id = $_arr_img['big_img_id'];
    				$img_thumb_id = $_arr_img['thumb_img_id'];
    				$img_small_id = $_arr_img['small_img_id'];
    			}
    			
    			$fabric_id = $this->newfabric($name, $img_url_id, $img_thumb_id, $img_small_id);
    			
    			if($this->item_fabric($item_id, $fabric_id)){					
					//echo $arr['img_show'];
					//return $this->render('imagefabric',[
						//	'model' => $model,
						//	'item_id' => $item_id,
						//	''
						//	]);
    			}else{
    				//echo '上传失败';
    			}
 			
    		}
    	}
    	
    	return $this->render('imagefabric', [
    			'model' => $model,
    			'item_id' => $item_id,
    			
    			]);
    }
   
    //*新增fabric并获取id
    public function newfabric($name,$img_url_id,$img_thumb_id,$img_small_id)
    {
		$model = new Fabric();
		$model->name = $name;
		$model->img_url_id = $img_url_id;
		$model->img_thumb_id = $img_thumb_id;
		$model->img_small_id = $img_small_id;
		
    	if($model->save())
    	{  	   	 
			return $model->id;
    	}
    }
    //在sys_item中添加加入新的fabric_list
    public function item_fabric($item_id,$fabric_id)
    {
    	$model = SysItem::findOne(['id' => $item_id]);
    	//$model->fabric_list = !empty($model->fabric_list) ? $model->fabric_list.','.$fabric_id : (string)$fabric_id;
		$model->fabric_list = $this->add_list($model->fabric_list, (string)$fabric_id);
    	if($model->save())
    	{
    		return true;
    	}
    	
    }
    /**
     * 新增材质
     * @param unknown $id
     * @return Ambigous <string, string>
     */
    public function actionImagematerial($item_id,$item_name,$class_name)
    {
    	$this->layout = 'main_small';
    	$model = new SelectImageForm();
    	$model->scenario = 'need_file_name';
    	 
    	$name = '';
    	if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
    		if($model->big_img_id == null && $model->b_t_s_img_id == null )
    		{
    			echo '<span class="btn btn-info">大图或大中小图必须选择一个</span>';
    		}
    		$name = $model->name;
    		$img_url_id = '';
    		$img_thumb_id = '';
    		$img_small_id = '';
    		
    		if ($model->validate() && ($model->big_img_id != null || $model->b_t_s_img_id != null)) {
    			 
    			 
    			if($model->b_t_s_img_id == null)
    			{
    				if($model->big_img_id != null ) $img_url_id = $model->big_img_id;
    				if($model->thumb_img_id != null ) $img_thumb_id = $model->thumb_img_id;
    				if($model->small_img_id != null ) $img_small_id = $model->small_img_id;
    			}
    			else
    			{
    					
    				$url = Image::findOne($model->b_t_s_img_id)->url;
    				$model_image = new Image();
        			$_arr_img = $model_image->get_img_id($url);
    				$img_url_id = $_arr_img['big_img_id'];
    				$img_thumb_id = $_arr_img['thumb_img_id'];
    				$img_small_id = $_arr_img['small_img_id'];
    			}
    					 
    			$material_id = $this->newmaterial($name, $img_url_id, $img_thumb_id, $img_small_id);
    			 
    			if($this->item_material($item_id, $material_id)){
    				//echo $arr['img_show'];
    				return $this->render('imagematerial',[
    						'model' => $model,
    						'item_id' => $item_id,
    						]);
    			}else{
    				//echo '上传失败';
    			}  
    		}
    	}
    	 
    	return $this->render('imagematerial', [
    			'model' => $model,
    			'item_id' => $item_id,
    			 
    			]);
    }
 
    //*新增material并获取id
    public function newmaterial($name, $img_url_id, $img_thumb_id, $img_small_id)
    {
    	$model = new Material();
    	$model->name = $name;
    	$model->img_url_id = $img_url_id;
    	$model->img_thumb_id = $img_thumb_id;
    	$model->img_small_id = $img_small_id;
    
    	if($model->save())
    	{
    		return $model->id;
    	}
    }
    //在sys_item中添加加入新的material_list
    public function item_material($item_id,$material_id)
    {
    	$model = SysItem::findOne(['id' => $item_id]);
    	//$model->material_list = !empty($model->material_list) ? $model->material_list.','.$material_id : (string)$material_id;
    	$model->material_list = $this->add_list($model->material_list, (string)$material_id);
    	
    	if($model->save())
    	{
    		return true;
    	}
    	 
    }
/**
 * 面料--材质关系对应图
 * @param unknown $id
 * @return Ambigous <string, string>
 */
    public function actionImagefm($item_id,$item_name,$class_name)
    {
    	$this->layout = 'main_small';
    	//$model = new SelectImageForm();
    	//$model->scenario = 'item_preview';
    	$model = new ItemPreviewDemo();
    	 
    	if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
    		if($model->big_img_id == null && $model->b_t_s_img_id == null )
    		{
    			echo '<span class="btn btn-info">大图或大中小图必须选择一个</span>';
    		}
    		if($model->fid == null && $model->mid == null )
    		{
    			echo  '<span class="btn btn-info">面料或材质需选择一个</span>'; 
    			
    		}
    		
    		$img_url_id = '';
    		$img_thumb_id = '';
    		$img_small_id = '';
    			
    		if ($model->validate() && ($model->big_img_id != null || $model->b_t_s_img_id != null)&& ($model->fid != null || $model->mid != null)) {
    			 			    			
    			if($model->b_t_s_img_id == null)
    			{
    				if($model->big_img_id != null ) $img_url_id = $model->big_img_id;
    				if($model->thumb_img_id != null ) $img_thumb_id = $model->thumb_img_id;
    				if($model->small_img_id != null ) $img_small_id = $model->small_img_id;
    			}
    			else
    			{
    					
    				$url = Image::findOne($model->b_t_s_img_id)->url;
    				$model_image = new Image();
        			$_arr_img = $model_image->get_img_id($url);
    				$img_url_id = $_arr_img['big_img_id'];
    				$img_thumb_id = $_arr_img['thumb_img_id'];
    				$img_small_id = $_arr_img['small_img_id'];
    			}
    			
    			
    			
    			$model_preview = new ItemPreview();
    			
    			$model_preview->img_url_id = $img_url_id;
    			$model_preview->img_thumb_id = $img_thumb_id;
    			$model_preview->img_small_id = $img_small_id;
    			$model_preview->item_id=$model->item_id;
    			$model_preview->fid=$model->fid;
    			$model_preview->mid=$model->mid;
    			

    			if($model_preview->save())
    			{
	    			return $this->render('imagefm', [
	    			'model' => $model,
	    			'item_id'=>$item_id,
	    			'item_name'=>$item_name,
	    			]);
    			}
    			else
    			{
    				//echo '上传失败';
    			}   			 
    		}
    	}
    	 
    	return $this->render('imagefm', [
    			'model' => $model,
    			'item_id'=>$item_id,
    			'item_name'=>$item_name,
    			]);
    }
    
   
/**
 * 
 * @param unknown $pid
 * @return Ambigous <string, string>
 */
    public function actionImagefmangle($pid,$item_id,$item_name,$class_name)
    {
    	$this->layout = 'main_small';
    	$model = new SelectImageForm();
    	$model->scenario = 'item_preview_angle';
    
    	if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
    		if($model->big_img_id == null && $model->b_t_s_img_id == null )
    		{
    			echo '<span class="btn btn-info">大图或大中小图必须选择一个</span>';
    		}
    		$img_url_id = '';
    		$img_thumb_id = '';
    		$img_small_id = '';
    		
    		if ($model->validate() &&($model->big_img_id != null || $model->b_t_s_img_id != null)) {
    			
    			if($model->b_t_s_img_id == null)
    			{
    				if($model->big_img_id != null ) $img_url_id = $model->big_img_id;
    				if($model->thumb_img_id != null ) $img_thumb_id = $model->thumb_img_id;
    				if($model->small_img_id != null ) $img_small_id = $model->small_img_id;
    			}
    			else
    			{
    					
    				$url = Image::findOne($model->b_t_s_img_id)->url;
    				$model_image = new Image();
        			$_arr_img = $model_image->get_img_id($url);
    				$img_url_id = $_arr_img['big_img_id'];
    				$img_thumb_id = $_arr_img['thumb_img_id'];
    				$img_small_id = $_arr_img['small_img_id'];
    			}
    			
    			$model_fmangle = new ItemPreviewAngle();
    			
    			$model_fmangle->img_url_id = $img_url_id;
    			$model_fmangle->img_thumb_id = $img_thumb_id;
    			$model_fmangle->img_small_id = $img_small_id;
    			$model_fmangle->pid = $pid;
    			    
    			if($model_fmangle->save())
    			{
					$this->redirect(['imagefm','item_id'=>$item_id,'item_name'=>$item_name,'class_name'=>$class_name]);
    			}
    			else
    			{
    				//echo '上传失败';
    			} 
    		}
    	}
    
    	return $this->render('imagefmangle', [
    			'model' => $model,
    			//'pid'=>$pid,
    			]);
    }
   
    /**
     * 删除默认图片，删除sys_item表中的记录
     * @param unknown $item_id
     * @return boolean
     */
    public function actionDeleteDefault($item_id)
    {
    	$model_item = SysItem::findOne(['id'=>$item_id]);
    	$model_item->img_url_id = 0;
    	$model_item->img_thumd_id = 0;
    	$model_item->img_small_id = 0; 
    	$model_item->save();
    	var_dump($model_item);
    	return true;    
    }
    /**
     * 删除面料，删除对应关系,再删除preview中的对应关系
     * @param unknown $fabric_id
     * @param unknown $item_id
     * @return boolean
     */
    public function actionDeleteFabric($fabric_id,$item_id)
    {

    	$model_item = SysItem::findOne(['id'=>$item_id]); 	
    	$model_item->fabric_list = $this->delete_list($model_item->fabric_list,(string)$fabric_id);
    	$model_item->save();
    	
    	ItemPreview::updateAll(['item_id'=>null],['item_id'=>$item_id,'fid'=>$fabric_id]);
    	    	
    	return true;   	
    	
    }
    /**
     * 删除材质，删除对应关系；再删除preview表中对应材质为的item_id = null
     * @param unknown $material_id
     * @param unknown $item_id
     * @return boolean
     */
    public function actionDeleteMaterial($material_id,$item_id)
    {
    	$model_item = SysItem::findOne(['id'=>$item_id]);  	 
    	$model_item->material_list = $this->delete_list($model_item->material_list, (string)$material_id);//删除在sys_item表的对应关系  	
    	$model_item->save();  
    	
		ItemPreview::updateAll(['item_id'=>null],['item_id'=>$item_id,'mid'=>$material_id]);
		
    	return true;
    	 
    }
    /**
     * 在base_sys_previw删除记录，但是在base_sys_preview_angle有依赖关系
     * @param unknown $preview_id
     * @return boolean
     */
    public function actionDeletePreview($preview_id)
    {
    	$model = ItemPreview::findOne(['id'=>$preview_id]);
    	$model_angel = ItemPreviewAngle::findAll(['pid'=>$preview_id]);
    	if($model_angel != null)
    	{
    		echo '需先删除依赖的其他角度照片';
    	}
    	else if($model->delete())
    	{
    		echo '删除';
    		return true;
    	}
    }
    /**
     * 删除其他角度图片
     * @param unknown $angle_id
     * @return boolean
     */
    public function actionDeleteAngle($angle_id)
    {
    	$model = ItemPreviewAngle::findOne(['id'=>$angle_id]);
    	if($model->delete()){
    		return true;
    	}

    }
    public function actionRelatedItem($item_id)
    {
    	$this->layout = 'main_small';
    	$searchModel = new SysItemSearch();
    	$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    	
    	return $this->render('related-item', [
    			'searchModel' => $searchModel,
    			'dataProvider' => $dataProvider,
    			]);
    }
    public function actionAddRelatedItem($item_id,$related_id)
    {
    	$model = SysItem::findOne(['id'=>$item_id]);
    	//$model->related_list = !empty($model->related_list) ? $model->related_list.','.$related_id : (string)$related_id;
    	//$model->related_list = $model->related_list.','.$related_id;
    	
    	$model->related_list = $this->add_list($model->related_list,$related_id);
    	$model->save();
    } 
    public function actionDeleteRelatedItem($item_id,$related_id)
    {
    	$model = SysItem::findOne(['id'=>$item_id]);
    	//$model->related_list = strpos($model->related_list,','.$related_id)?str_replace(','.$related_id, '', $model->related_list):str_replace($related_id, '', $model->related_list);
    	
    	//if(strpos($model->related_list,','.$related_id)){str_replace(','.$related_id, '', $model->related_list);}
    	//if(strpos($model->related_list,$related_id)){str_replace($related_id, '', $model->related_list);}
    	
    	$model->related_list = $this->delete_list($model->related_list,$related_id);
    	$model->save();
    }
    /**
     * 添加值到list中，如果存在，则不添加
     * @param unknown $list
     * @param unknown $data
     * @return string
     */
    public function add_list($list,$data)
    {
    	
    	if(!empty($list))
    	{
    		$arr = explode(',', $list);
    		if(!in_array($data, $arr))
    		{
    			array_push($arr, $data);
    			return implode(',', $arr);
    		}
    		return $list;
    	}
		else 
		{
			return $data;
		}
    		
    }
    /**
     * 从list中删除值
     * @param unknown $list
     * @param unknown $data
     * @return string
     */
    public function delete_list($list,$data)
    {
    	$arr = explode(',', $list);
    	$arr_push = array();
    	
    	foreach ($arr as $value)
    	{
    		if($value != $data)
    		{
    			array_push($arr_push, $value);
    		}
    	}
    	return implode(',', $arr_push);
    }
    /**
     	待完善
     * @return Ambigous <string, string>
     */
    public function actionFileManage()
    {
    	$model = PrfType::find()->all();
    	//var_dump($model);
    	
    	////foreach ($model as $value)
    //	{
    		//$arr = explode('/', $value);
    		

   	//	}
    	return $this->render('file_manage',[
    			'model' => $model,
    			]);
    	
    }

    /**
     * 父框架报上各个图片上传的页面
     * @param unknown $id
     */
    public function actionImageitem($id){
    	$this->render('imageitem',[
    			'id' =>$id,
    			
    			]);
    }
    
    public function actionAddCustomPrice($item_id,$color_id=2,$fabric_id=null,$material_id=null,$price_custom=null,$price_discount=null)
    {

    	$model_price = SysItemPrices::findOne([
    		'item_id'=>$item_id,
    		'color_id'=>$color_id,
    		'fabric_id'=>$fabric_id,
    		'material_id'=>$material_id
    		]
    	);
    	
    	if(!empty($model_price))
    	{
    		return '1';
    		//return $this->render('test');
    		
    	}
    	
    	$model = new SysItemPrices();
    	$model->item_id = $item_id;
    	$model->color_id = $color_id;
    	$model->fabric_id = $fabric_id;
    	$model->material_id = $material_id;
    	$model->price_custom = $price_custom;
    	$model->price_discount = $price_discount;
    	if($model->save()){
  			//return 'ok';
  			
    	}
    	
    	
    }
    /**
     * 删除定制价格
     * @param unknown $id
     */
    public function actionDeleteCustomPrice($id)
    {
    	if (($model = SysItemPrices::findOne($id)) !== null) {
    		$model->delete();
    	}
    }
    public function actionAddGjjCode($id)
    {
    	$date = date('ym',time());   	
    	$model = SysItem::findOne(['id'=>$id]);
    	$factory_short_code =  $model->brand_name->brandFactory->short_code;
    	   	
    	$class2= '';
    	if(strpos($model->class_id, '000000'))//只有二级
    	{
    		$class2 = $model->class_id;
    	}else if(GrpClass::findOne(['id'=>$model->class_id]) != null){
    		$class2 = GrpClass::findOne(['id'=>$model->class_id])->father;//有三级
    	}
    	
    	$gjj_code_class2 =  str_pad(ceil($class2/1000000), 4, '0' ,STR_PAD_LEFT);
    	$gjj_code_id = str_pad($id, 11, '0' ,STR_PAD_LEFT);
    	
    	if($model->gjj_code == null)
    	{
    		$model->gjj_code = $date . $factory_short_code . $gjj_code_class2 . $gjj_code_id;
    		$model->app_return = '1';
    		if($model->save()) 
    		return '过++代码设置成功';
    	}
    	//return '过++代码已存在';
    }
    public function update_gjj_code($id)
    {
    	$date = date('ym',time());
    	$model = SysItem::findOne(['id'=>$id]);
    	$factory_short_code =  $model->brand_name->brandFactory->short_code;
    	$class2= '';
    	if(strpos($model->class_id, '000000'))//只有二级
    	{
    		$class2 = $model->class_id;
    	}else if(GrpClass::findOne(['id'=>$model->class_id]) != null) {
    		$class2 = GrpClass::findOne(['id'=>$model->class_id])->father;//有三级
    	}
    	
    	$gjj_code_class2 =  str_pad(ceil($class2/1000000), 4, '0' ,STR_PAD_LEFT);
    	$gjj_code_id = str_pad($id, 11, '0' ,STR_PAD_LEFT);
    	return $date . $factory_short_code . $gjj_code_class2 . $gjj_code_id;
    }
}

